package com.guqj.mq.activitemq.queue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息生产者
 * 
 * @author guqj
 * 
 */
public class JmsProducer {

	// null
	private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
	// null
	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
	// failover://tcp://localhost:61616
	private static final String BOKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;

	/**
	 * 创建Session
	 * 
	 * @return
	 * @throws JMSException
	 */
	public Session createSession() throws JMSException {
		// 实例化连接工厂
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				USERNAME, PASSWORD, "failover:(tcp://localhost:61616,tcp://localhost:61617)?randomize=false");
		// 通过连接工厂获取连接
		Connection connection = connectionFactory.createConnection();
		// 启动连接
		connection.start();
		// 创建session
		Session session = connection.createSession(Boolean.TRUE,
				Session.AUTO_ACKNOWLEDGE);
		return session;
	}

	/**
	 * 获取消息生产者
	 * 
	 * @param session
	 * @param queue
	 * @return
	 * @throws JMSException
	 */
	public MessageProducer createProducer(Session session, String queue)
			throws JMSException {
		// 创建队列
		Destination destination = session.createQueue(queue);
		// 创建生产者
		MessageProducer messageProducer = session.createProducer(destination);
		messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
		return messageProducer;
	}

	/**
	 * 发生消息
	 * 
	 * @param session
	 * @param messageProducer
	 * @param message
	 * @throws JMSException
	 */
	public void sendMessage(Session session, MessageProducer messageProducer,
			String message) throws JMSException {
		TextMessage textMessage = session.createTextMessage(message);
		textMessage.setStringProperty("JMSXGroupID", "OrderGroup-A");
		messageProducer.send(textMessage);
		System.out.println("发送消息：" + textMessage.getText());
	}

	public void sendMessage2(Session session, MessageProducer messageProducer,
			String message) throws JMSException {
		TextMessage textMessage = session.createTextMessage(message);
		textMessage.setStringProperty("JMSXGroupID", "OrderGroup-B");
		messageProducer.send(textMessage);
		System.out.println("发送消息：" + textMessage.getText());
	}
	
	public static void main(String[] args) {
		String queue = "Order.Group";
		JmsProducer p = new JmsProducer();
		Session session = null;
		try {
			session = p.createSession();
			MessageProducer producer = p.createProducer(session, queue);
			for (int i = 1; i <= 3; i++) {
				p.sendMessage(session, producer, "Message From OrderGroup-A："  + i);
				p.sendMessage2(session, producer, "Message From OrderGroup-B："  + i);
			}
			session.commit();
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != session) {
					session.close();
				}
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
}
